package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.sl.au.entity.*;
import com.sl.au.repository.AfterSaleActiveRepository;
import com.sl.au.service.AfterSaleActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.service.ExcelService;
import org.springframework.util.StringUtils;

@Service("afterSaleActiveService")
@Transactional
public class AfterSaleActiveServiceImpl extends BaseServiceImpl implements AfterSaleActiveService {

    @Autowired
    AfterSaleActiveRepository afterSaleActiveRepository;

    @Autowired
    ExcelService excelService;


    @Override
    public List<ContractVo> getPage(int start, int length, String key, String orderName, String dir) {
        Page<Contract> pages = null;
        Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
        pages = afterSaleActiveRepository.getPage(key, pageable);
        List<Contract> contracts = pages.getContent();
        List<ContractVo> contractVos = new ArrayList<>();
        for(Contract contract : contracts){
            ContractVo contractVo = new ContractVo();
            contractVo = setContractVoData(contract);
            Date servicingTime = new Date();//最早维护时间
            contractVo.setServicingTime(servicingTime);
            List<MaintainItemDevice> mids = this.afterSaleActiveRepository
                                .getMaintainPlansByContractID(contract.getId());
            String state = "正常";
            if(mids.size()>0) {
                if (mids.get(0).getStartDate()!=null) {
                    Date startDate = mids.get(0).getStartDate();
                    int day = mids.get(0).getMaintainPeriod();//最早的计划的维护周期
                    long time = startDate.getTime(); // 得到出厂日期的毫秒数
                    long dayTime = 24 * 60 * 60 * 1000; // 一天的时间转换成毫秒数
                    time += day * dayTime; // 相加得到新的毫秒数
                    startDate = new Date(time); // 将毫秒数转换成日期
                    contractVo.setServicingTime(startDate);
                    long nowTime = servicingTime.getTime();
                    if (time < nowTime) {
                        state = "已超期";
                    }
                }
            }
            contractVo.setState(state);
            contractVos.add(contractVo);
        }
        return contractVos;
    }

    @Override
    public long getPageCount(String key) {
        return this.afterSaleActiveRepository.getPageCount(key);
    }

    @Override
    public List<MaintainItemDeviceVo> getPlanPage(int start, int length, String key, String orderName, String dir) {
        Page<MaintainItemDevice> pages = null;
        Pageable pageable = new PageRequest(start / length, length);
        pages = afterSaleActiveRepository.getPlanPage(key, pageable);
        List<MaintainItemDevice> maintainItemDevices = pages.getContent();
        List<MaintainItemDeviceVo> maintainItemDeviceVos = new ArrayList<>();
        for(MaintainItemDevice maintainItemDevice : maintainItemDevices) {
            MaintainItemDeviceVo maintainItemDeviceVo = new MaintainItemDeviceVo();
            maintainItemDeviceVo = setMaintainItemDeviceVoData(maintainItemDevice);
            String state = "正常";
            if (maintainItemDevice.getStartDate()!=null) {
                Date nowDate = new Date();
                Date startDate = maintainItemDevice.getStartDate();
                int day = maintainItemDevice.getMaintainPeriod();//最早的计划的维护周期
                long time = startDate.getTime(); // 得到日期的毫秒数
                long dayTime = 24 * 60 * 60 * 1000; // 一天的时间转换成毫秒数
                time += day * dayTime; // 相加得到新的毫秒数
                long nowTime = nowDate.getTime();
                if (time < nowTime) {
                    state = "已超期";
                }
                maintainItemDeviceVo.setState(state);
                maintainItemDeviceVos.add(maintainItemDeviceVo);
            }
        }
        return maintainItemDeviceVos;
    }

    @Override
    public long getPlanPageCount(String key) {
        return this.afterSaleActiveRepository.getPlanPageCount(key);
    }


    private MaintainItemDeviceVo setMaintainItemDeviceVoData(MaintainItemDevice maintainItemDevice) {
        MaintainItemDeviceVo maintainItemDeviceVo = new MaintainItemDeviceVo();
        if (StringUtils.hasText(maintainItemDevice.getId())){
            maintainItemDeviceVo.setPlanId(maintainItemDevice.getId());
        }
        if (maintainItemDevice.getCreateTime() !=null){
            maintainItemDeviceVo.setCreateTime(maintainItemDevice.getCreateTime());
        }
        if (StringUtils.hasText(maintainItemDevice.getContractID())){
            maintainItemDeviceVo.setContractID(maintainItemDevice.getContractID());
        }
        if (StringUtils.hasText(maintainItemDevice.getLeaveFactoryCode())){
            maintainItemDeviceVo.setLeaveFactoryCode(maintainItemDevice.getLeaveFactoryCode());
        }
        if (maintainItemDevice.getStartDate() !=null){
            maintainItemDeviceVo.setStartDate(maintainItemDevice.getStartDate());
        }
        maintainItemDeviceVo.setMaintainPeriod(maintainItemDevice.getMaintainPeriod());
        if (StringUtils.hasText(maintainItemDevice.getMaintainProject())){
            maintainItemDeviceVo.setMaintainProject(maintainItemDevice.getMaintainProject());
        }
        if (StringUtils.hasText(maintainItemDevice.getAnnouncement())){
            maintainItemDeviceVo.setAnnouncement(maintainItemDevice.getAnnouncement());
        }
        if (StringUtils.hasText(maintainItemDevice.getMemo())){
            maintainItemDeviceVo.setMemo(maintainItemDevice.getMemo());
        }
        return maintainItemDeviceVo;
    }


    private ContractVo setContractVoData(Contract contract) {
        ContractVo contractVo = new ContractVo();
        if (StringUtils.hasText(contract.getId())){
            contractVo.setContractId(contract.getId());
        }
        if (contract.getCreateTime() != null){
            contractVo.setCreateTime(contract.getCreateTime());
        }
        if (StringUtils.hasText(contract.getSupplierCode())){
            contractVo.setSupplierCode(contract.getSupplierCode());
        }
        if (StringUtils.hasText(contract.getDemanderCode())){
            contractVo.setDemanderCode(contract.getDemanderCode());
        }
        if (StringUtils.hasText(contract.getContractType())){
            contractVo.setContractType(contract.getContractType());
        }
        contractVo.setTotalSum(contract.getTotalSum());
        if (contract.getSignedDate() != null){
            contractVo.setSignedDate(contract.getSignedDate());
        }
        if (contract.getScheduleDate() != null){
            contractVo.setScheduleDate(contract.getScheduleDate());
        }
        if (contract.getDeliveryDate() != null){
            contractVo.setDeliveryDate(contract.getDeliveryDate());
        }
        if (StringUtils.hasText(contract.getStop())){
            contractVo.setStop(contract.getStop());
        }
        contractVo.setFreightCharge(contract.getFreightCharge());
        if (StringUtils.hasText(contract.getProjectName())){
            contractVo.setProjectName(contract.getProjectName());
        }
        if (StringUtils.hasText(contract.getPayType())){
            contractVo.setPayType(contract.getPayType());
        }
        if (StringUtils.hasText(contract.getGuaranteePeriod())){
            contractVo.setGuaranteePeriod(contract.getGuaranteePeriod());
        }
        if (StringUtils.hasText(contract.getBreakDuty())){
            contractVo.setBreakDuty(contract.getBreakDuty());
        }
        if (StringUtils.hasText(contract.getOrderUnit())){
            contractVo.setOrderUnit(contract.getOrderUnit());
        }
        if (StringUtils.hasText(contract.getOrderDelegate())){
            contractVo.setOrderDelegate(contract.getOrderDelegate());
        }
        if (StringUtils.hasText(contract.getOrderPhone())){
            contractVo.setOrderPhone(contract.getOrderPhone());
        }
        if (StringUtils.hasText(contract.getIndustryInfo())){
            contractVo.setIndustryInfo(contract.getIndustryInfo());
        }
        if (StringUtils.hasText(contract.getOrderAddress())){
            contractVo.setOrderAddress(contract.getOrderAddress());
        }
        contractVo.setOrderZip(contract.getOrderZip());
        if (StringUtils.hasText(contract.getOrderArea())){
            contractVo.setOrderArea(contract.getOrderArea());
        }
        if (StringUtils.hasText(contract.getPostUnit())){
            contractVo.setPostUnit(contract.getPostUnit());
        }
        if (StringUtils.hasText(contract.getPostDelegate())){
            contractVo.setPostDelegate(contract.getPostDelegate());
        }
        if (StringUtils.hasText(contract.getPostPhone())){
            contractVo.setPostPhone(contract.getPostPhone());
        }
        if (StringUtils.hasText(contract.getPostAddress())){
            contractVo.setPostAddress(contract.getPostAddress());
        }
        contractVo.setPostZip(contract.getPostZip());
        if (StringUtils.hasText(contract.getDutyArea())){
            contractVo.setDutyArea(contract.getDutyArea());
        }
        if (StringUtils.hasText(contract.getDutyOfficer())){
            contractVo.setDutyOfficer(contract.getDutyOfficer());
        }
        if (StringUtils.hasText(contract.getSouthNorthArea())){
            contractVo.setSouthNorthArea(contract.getSouthNorthArea());
        }
        if (StringUtils.hasText(contract.getBelongArea())){
            contractVo.setBelongArea(contract.getBelongArea());
        }
        if (StringUtils.hasText(contract.getCheckKind())){
            contractVo.setCheckKind(contract.getCheckKind());
        }
        if (StringUtils.hasText(contract.getProductKind())){
            contractVo.setProductKind(contract.getProductKind());
        }
        if (StringUtils.hasText(contract.getSignOfficer())){
            contractVo.setSignOfficer(contract.getSignOfficer());
        }
        if (StringUtils.hasText(contract.getRepairOfficer())){
            contractVo.setRepairOfficer(contract.getRepairOfficer());
        }
        if (StringUtils.hasText(contract.getQuotationOfficer())){
            contractVo.setQuotationOfficer(contract.getQuotationOfficer());
        }
        if (StringUtils.hasText(contract.getOperatDepartment())){
            contractVo.setOperatDepartment(contract.getOperatDepartment());
        }
        if (StringUtils.hasText(contract.getOperator())){
            contractVo.setOperator(contract.getOperator());
        }
        if (contract.getOperatDate() != null){
            contractVo.setOperatDate(contract.getOperatDate());
        }
        if (StringUtils.hasText(contract.getMemo())){
            contractVo.setMemo(contract.getMemo());
        }
        if (StringUtils.hasText(contract.getName())){
            contractVo.setName(contract.getName());
        }
        if (StringUtils.hasText(contract.getProjectCode())){
            contractVo.setProjectCode(contract.getProjectCode());
        }
        if (StringUtils.hasText(contract.getRepairCode())){
            contractVo.setRepairCode(contract.getRepairCode());
        }
        contractVo.setRepairNumber(contract.getRepairNumber());
        if (StringUtils.hasText(contract.getRepairKind())){
            contractVo.setRepairKind(contract.getRepairKind());
        }
        if (StringUtils.hasText(contract.getRepairKind())){
            contractVo.setRepairKind(contract.getRepairKind());
        }
        if (contract.getRepairDate() != null){
            contractVo.setRepairDate(contract.getRepairDate());
        }
        return contractVo;
    }

    @Override
     public List<AfterSaleActive> getAfterSaleActives(int start, int length, String key, String orderName, String dir) {
        Page<AfterSaleActive> pages = null;
        Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
        pages = afterSaleActiveRepository.getName(key, pageable);
        return pages.getContent();
    }
    public AfterSaleActiveRepository getRepository() {
        return this.afterSaleActiveRepository;
    }

	@Override
	public List<ContractVo> getAllContract() {
		Page<Contract> pages = null;
        Pageable pageable = new PageRequest(0, Integer.MAX_VALUE, Sort.Direction.DESC, "createTime");
        pages = afterSaleActiveRepository.getPage("", pageable);
        List<Contract> contracts = pages.getContent();
        List<ContractVo> contractVos = new ArrayList<>();
        for(Contract contract : contracts){
            ContractVo contractVo = new ContractVo();
            contractVo = setContractVoData(contract);
            Date servicingTime = new Date();//最早维护时间
            contractVo.setServicingTime(servicingTime);
            List<MaintainItemDevice> mids = this.afterSaleActiveRepository
                                .getMaintainPlansByContractID(contract.getId());
            String state = "正常";
            if(mids.size()>0) {
                if (mids.get(0).getStartDate()!=null) {
                    Date startDate = mids.get(0).getStartDate();
                    int day = mids.get(0).getMaintainPeriod();//最早的计划的维护周期
                    long time = startDate.getTime(); // 得到出厂日期的毫秒数
                    long dayTime = 24 * 60 * 60 * 1000; // 一天的时间转换成毫秒数
                    time += day * dayTime; // 相加得到新的毫秒数
                    startDate = new Date(time); // 将毫秒数转换成日期
                    contractVo.setServicingTime(startDate);
                    long nowTime = servicingTime.getTime();
                    if (time < nowTime) {
                        state = "已超期";
                        contractVo.setState(state);
                        contractVos.add(contractVo);
                    }
                }
            }
            
        }
        return contractVos;
	}
}
